import axios from 'axios';

export function requestLogin(params) {
    const username = params.username
    const password = params.password
    const ps = new FormData();
    ps.append('username', params.username);
    ps.append('password', params.password);
    return axios.post("/login",params).then(response => response).catch(error => error);
}

export function requestSendCode(mobile) {
    const headerOptions = {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    };
    const ps = new FormData();
    ps.append('mobile', mobile);
    var json = {'mobile':mobile};
    return axios.post("/erm/login/resetPwdReq?v=v1.0", json, headerOptions).then(response => response).catch(errot=> error);
}

export function resetPsw(params) {
    const headerOptions = {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    };
    const ps = new FormData();
    ps.append('mobile', params.mobile);
    ps.append('code', params.code);
    ps.append('password', params.password);
    return axios.post("/erm/login/resetPwd", params, headerOptions).then(response => response).catch(errot=> error);
}

  export function getAllPermissions() {
    var cuinfo = user();
    return axios.get("/erm/role/getFunctionList?v=v1.0&enterpriseId=" + cuinfo.enterpriseId + "&t=" + new Date().getTime()).then(response => response).catch(errot=> error);
  }

  export function getRoleGroup(rgid) {
    return axios.get("/erm/role/getRoleGroup?v=v1.0&id=" + rgid + "&t=" + new Date().getTime()).then(response => response).catch(errot=> error);
  }

  export function getRoles() {
    return axios.get("/erm/role/getRoleGroups?v=v1.0" + "&t=" + new Date().getTime()).then(response => response).catch(errot=> error);
  }

  export function getRoleMember(roleId) {
    return axios.get("/erm/role/getRoleGroupMembers?v=v1.0&roleGroupId=" + roleId + "&t=" + new Date().getTime()).then(response => response).catch(errot=> error);
  }

  export function addRole(params) {
    const headerOptions = {
        headers: {
            "Content-Type": "application/json"
        }
    };
    const ps = new FormData();
    ps.append('name', params.name);
    ps.append('roles', params.roles);
    return axios.post("/erm/role/createRoleGroup?v=v1.0", params, headerOptions).then(response => response).catch(errot=> error);
  }

  export function editRole(params) {
    const headerOptions = {
        headers: {
            "Content-Type": "application/json"
        }
    };
    // const ps = new FormData();
    // ps.append('id', params.id);
    // ps.append('name', params.name);
    // ps.append('roles', params.roles);
    // ps.append('status', params.status);

    return axios.post("/erm/role/editRoleGroup?v=v1.0", params, headerOptions).then(response => response).catch(errot=> error);
  }

  export function delRole(roleId) {
    const headerOptions = {
        headers: {
            'Content-Type': 'multipart/form-data'
        }
    };
    return axios.delete("/erm/role/delRoleGroup?v=v1.0&id=" + roleId, headerOptions).then(response => response).catch(error=> error);
  }
  export function getUserPartyTree() {
    return axios.get("/erm/party/getUserPartyTree?v=v1.0" + "&t=" + new Date().getTime()).then(response => response).catch(error=> error);
  }
  export function getEntityListForUser() {
    return axios.get("/user/userSimpleEntityList?v=v1.0" + "&t=" + new Date().getTime()).then(response => response).catch(error=> error);
  }